﻿using HpxWMS_Api.CustomExcption;
using HpxWMS_Api.DAL;
using HpxWMS_Api.Entities;
using HpxWMS_Api.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HpxWMS_Api.BLL
{
    /// <summary>
    /// 仓库业务逻辑层
    /// </summary>
    public class WarehouseManage
    {
        private WarehouseServices warehouseservices = new WarehouseServices();
        private AccountServices accountServices = new AccountServices();
        /// <summary>
        /// 查找一页数据
        /// </summary>
        /// <returns></returns>
        public List<WarehouseDTO> FindPage(Wq wq)
        {
            var list = warehouseservices.FindPage(wq);
            return list.Select(warehouse => new WarehouseDTO()
            {
                Id = warehouse.Id,
                Name = warehouse.Name,
                Address = warehouse.Address,
                ManageId = warehouse.UserId,
                ManageName = warehouse.Manager.NickName,
                Mobile = warehouse.Mobile
            }).ToList();
        }
        /// <summary>
        /// 返回符合条件的记录数
        /// </summary>
        /// <returns></returns>
        public int Count(string searchText)
        {
            return warehouseservices.Count(searchText);
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="wi"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool Insert(Wi wi)
        {
            //判断仓库名是否重复
            if (warehouseservices.ExictBywareName(wi.Name))
            {
                throw new BusinessException("020", "添加失败，仓库名已存在");
            }
            if (!accountServices.ExictByUserId(wi.ManageId))
            {
                throw new BusinessException("021", "添加失败，仓库管理员不存在");
            }

            return warehouseservices.Insert(wi);

        }
        /// <summary>
        /// 更新仓库
        /// </summary>
        /// <param name="wu"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool Update(Wu wu)
        {
            if (warehouseservices.ExictBywareNameandNotEqualId(wu.Name, wu.Id))
            {
                throw new BusinessException("022","更新失败，仓库名已存在");
            }
            if (!accountServices.ExictByUserId(wu.ManageId))
            {
                throw new BusinessException("021", "添加失败，仓库管理员不存在");
            }
            return warehouseservices.Updata(wu);
        }
        /// <summary>
        /// 删除仓库
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool Remove(int id)
        {
            return warehouseservices.Remove(id);
        }
    }
}